﻿Imports System
Imports System.Xml
Imports System.IO
Public Class Form1
    Friend SelectedTitleID, SelectedGameName, xmlLocation As String
    Friend Gamecount As Integer
    Friend finished As Boolean = False
    Private Sub OpenContentButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenContentButton.Click
        Try
            OpenFileDialog1.Filter = ".xml|*.xml|All files|*.*"
            OpenFileDialog1.FileName = "Content.xml"
            If OpenFileDialog1.ShowDialog = vbOK Then
                xmlLocation = OpenFileDialog1.FileName

                XMLStuff.CheckXMLIntegrity(xmlLocation)

                XMLStuff.InitialXMLRead(xmlLocation)

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
            GamesListView.Items.Clear()
        End Try

    End Sub

    Private Sub GetTUButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetTUButton.Click
        Dim i = 0
        Dim count As Integer = 1

        'check if at least one game has been selected
        If GamesListView.Items.Count >= 0 And GamesListView.SelectedItems.Count <> 0 Then
            'check if the loop item has been checked


            Gamecount = GamesListView.SelectedItems.Count

            Progress.ProgressBar1.Maximum = Gamecount
            Progress.ProgressBar1.Value = 0

            Progress.TotalGamesLabel.Text = Nothing
            Progress.GameNameLabel.Text = Nothing
            Progress.webaddressLabel.Text = Nothing
            Progress.GameNumberLabel.Text = Nothing

            finished = False
            Progress.Show()
            Do
                If GamesListView.Items(i).Selected <> False Then

                    SelectedTitleID = GamesListView.Items(i).SubItems.Item(1).Text
                    SelectedGameName = GamesListView.Items(i).SubItems.Item(0).Text

                    Dim webpath As String = "http://marketplace.xboxmodder.com.nyud.net/dlc/" & SelectedTitleID & "/000B0000/"
                    Dim DestPath As String = "TU_Temp_List"

                    Progress.TotalGamesLabel.Text = "of " & Gamecount.ToString
                    Progress.GameNameLabel.Text = SelectedGameName.ToString
                    Progress.webaddressLabel.Text = webpath.ToString
                    Progress.GameNumberLabel.Text = count.ToString
                    Progress.ProgressBar1.Value = count.ToString
                    Progress.Refresh()
                    Progress.Focus()

                    If NetStuff.DownloadFile(webpath, DestPath) = True Then

                        Dim filea As String

                        Dim oReada As System.IO.StreamReader
                        oReada = File.OpenText("TU_Temp_List")

                        oReada.BaseStream.Seek(55, SeekOrigin.Begin)
                        filea = oReada.ReadToEnd

                        My.Computer.FileSystem.WriteAllText("TU_Temp_List.xml", filea, False)
                        oReada.Close()
                        Dim xDoc As XmlDocument = New XmlDocument()
                        xDoc.Load("TU_Temp_List.xml")

                        Dim name As XmlNodeList = xDoc.GetElementsByTagName("li")

                        For index = 1 To name.Count - 1
                            webpath = "http://marketplace.xboxmodder.com.nyud.net/dlc/" & SelectedTitleID & "/000B0000/" & name(index).InnerText.ToString.Remove(0, 1)
                            If Not File.Exists("cache/" & name(index).InnerText.ToString.Remove(0, 1)) Then
                                DestPath = "cache/" & name(index).InnerText.ToString.Remove(0, 1)
                                Progress.webaddressLabel.Text = webpath.ToString
                                Progress.Refresh()
                                Progress.Focus()
                                NetStuff.DownloadFile(webpath, DestPath)
                            Else
                                LogStuff.ErrorLogWrite("TU already exists", "cache/" & name(index).InnerText.ToString.Remove(0, 1))
                            End If

                            'MessageBox.Show("Name: " & name(index).InnerText.ToString)
                        Next
                    End If
                    count += 1
                End If
                'increase counter
                i += 1

                'loop until all games have been checked
            Loop Until i = GamesListView.Items.Count
            finished = True
            Progress.Close()
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Asterisk)
        Else
            'if no games where checked then display error message and exit loop
            ''need to fix this up
            MessageBox.Show("No Games were selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Directory.Exists("cache") Then
            Directory.CreateDirectory("cache")
        End If

        LogListBox.Items.Add("Title Downloader has started")
        LogListBox.Items.Add("   Scuba156 (Xbox1Crew, XeLove)")
        LogListBox.Items.Add("   Select 'open Content.xml' to start")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        XMLStuff.AddTUtoLog("", "")
    End Sub

    Private Sub GetAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetAllButton.Click
        Dim webpath As String = "http://marketplace.xboxmodder.com.nyud.net/updates.xml"
        NetStuff.DownloadFile(webpath, "TU_DB.xml")

        Dim TUXML As XmlDocument = New XmlDocument()

        TUXML.Load("TU_DB.xml")
        Dim FolderPath As XmlNodeList = TUXML.GetElementsByTagName("folder")
        Dim FilePath As XmlNodeList = TUXML.GetElementsByTagName("filename")
        For indexcount = 0 To FolderPath.Count - 1

            NetStuff.DownloadFile(FolderPath(indexcount).InnerText.ToString & FilePath(indexcount).InnerText.ToString, "Cache")
        Next

    End Sub
End Class
